﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using PlanoAulaHelper.Reports;

namespace PlanoAulaHelper
{
    public partial class PlanoAulaCabForm : FormEdicao
    {

        public PlanoAulaCabForm()
            : this(0)
        {
       
        }
        public PlanoAulaCabForm(int idPlanoAula) : base( idPlanoAula )
        {
            InitializeComponent();            
        }
        private void edtSemestre_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            e.IsInputKey = Char.IsNumber((char)e.KeyValue);
        }
        private void edtDe_ValueChanged(object sender, EventArgs e)
        {
            if (edtAte.Value < edtDe.Value)
            {
                edtAte.Value = edtDe.Value.AddMonths(4);
            }
        }

        private void btnSalvar_Click(object sender, EventArgs e)
        {
            using (dbContext dc = new dbContext())
            {
                System.Data.Linq.Table<PlanoAulaCab> table = dc.PlanoAulaCabs;
                var cpr = (from sel in table
                           where sel.Id == IdItem
                           select sel).SingleOrDefault<PlanoAulaCab>();
                bool novo = cpr == null;
                if (novo)
                {
                    cpr = new PlanoAulaCab();
                }

               
                cpr.Materia = edtMateria.Text;
                cpr.Universidade = edtUniversidade.Text;
                cpr.De = edtDe.Value;
                cpr.Ate = edtAte.Value;
                cpr.Curso = edtCurso.Text;
                cpr.Professor = edtProfessor.Text;
                cpr.Semestre = edtSemestre.Text.To<int?>("Informe um semestre válido.",null);
                cpr.Ano = Convert.ToInt32(edtAno.Text);
                cpr.CargaHoraria = Convert.ToInt32(edtCargaHoraria.Text);
                cpr.ObjetivoEspecifico = edtObjetivosEspecificos.Text;
                cpr.ObjetivoGeral = edtObjetivosGerais.Text;

                if (novo)
                {
                    dc.PlanoAulaCabs.InsertOnSubmit(cpr);
                }
                
                dc.SubmitChanges();
                this.IdItem = cpr.Id;
            }
            AtualizarTela();
        }
        PlanoAulaCab planoAula = null;
        private void PlanoAulaCabForm_Load(object sender, EventArgs e)
        {
            //CarregarDiasDaSemana();
            AtualizarTela();
        }

        private void AtualizarTela()
        {
            System.Data.Linq.Table<PlanoAulaCab> table = new dbContext().PlanoAulaCabs;
            var c = (from sel in table
                     where sel.Id == IdItem
                     select sel).SingleOrDefault<PlanoAulaCab>();
            planoAula = c;
            if (c != null)
            {
                edtAte.Value = c.Ate;
                edtDe.Value = c.De;
                edtUniversidade.Text = c.Universidade;
                edtMateria.Text = c.Materia;

                edtCurso.Text = c.Curso;
                edtProfessor.Text = c.Professor;
                edtSemestre.Text = c.Semestre.ToString().PadLeft(2, '0');
                edtAno.Text = c.Ano.ToString();
                edtCargaHoraria.Text = c.CargaHoraria.ToString().PadLeft(3, '0');
                edtObjetivosGerais.Text = c.ObjetivoGeral;
                edtObjetivosEspecificos.Text = c.ObjetivoEspecifico;
            }
            else
            {
                edtSemestre.Text = DateTime.Today.Month >= 7 ? "02" : "01";
                edtAno.Text = DateTime.Today.Year.ToString();
                edtAte.Value = edtDe.Value.AddMonths(4);
            }

            HabilitarBotao(c != null);
        }
        private void HabilitarBotao(bool habilitar)
        {
            toolStripDropDownButtonOpcoes.Enabled = habilitar;
            toolStripButtonVisualizar.Enabled = habilitar;
            diasDeAulaToolStripMenuItem.Enabled = habilitar;
        }
        //private void CarregarDiasDaSemana()
        //{
        //    cbxDiaAula.Items.Clear();
        //    for (int i = 1; i < 7; i++)
        //    {
        //        cbxDiaAula.Items.Add(Enum.Parse(typeof(DayOfWeek), i.ToString()));
        //    }
        //    cbxDiaAula.Items.Add(Enum.Parse(typeof(DayOfWeek), "0"));
        //}

        private void cbxDiaAula_Format(object sender, ListControlConvertEventArgs e)
        {
            e.Value = CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedDayNames[(int)((DayOfWeek)e.ListItem)];
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            new ConteudoPlanoAulaForm(planoAula).ExbirFormulario(this);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButtonAvaliacao_Click(object sender, EventArgs e)
        {
            new UnidadeAvaliacaoForm(planoAula.Id).ExbirFormulario(this);
        }

        private void toolStripButtonVisualizar_Click(object sender, EventArgs e)
        {
            new relPlanoAulaForm(planoAula.Id).ExbirFormulario(this);
        }

        private void diasDeAulaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new DiasEHorarioEnsinoForm(planoAula.Id).ExbirFormulario(this);
            
        }
    }
}
